package eshin.blog.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;

import eshin.blog.beans.User;
import eshin.blog.service.UserService;
@Controller("zhuCeAction")
@Scope("prototype")
public class ZhuCeAction{
	private String username;
	private String password;
	private String repassword;
	private String nickname;
	private String question;
	private String answer;
	@Resource private UserService userService;
	private String verifyCode;
	
	public String getVerifyCode() {
		return verifyCode;
	}
	public void setVerifyCode(String verifyCode) {
		this.verifyCode = verifyCode;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getRepassword() {
		return repassword;
	}
	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}
	public String getNickname() {
		return nickname;
	}
	public void setNickname(String nickname) {
		this.nickname = nickname;
	}
	public String getQuestion() {
		return question;
	}
	public void setQuestion(String question) {
		this.question = question;
	}
	public String getAnswer() {
		return answer;
	}
	public void setAnswer(String answer) {
		this.answer = answer;
	}
	
	
	public String execute(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String session_vcode = (String)request.getSession().getAttribute("vCode");
		try {
			//锟斤拷装一锟斤拷user锟斤拷锟斤拷
			User user = new User();
			user.setUsername(username);
			user.setPassword(password);
			user.setNickname(nickname);
			user.setQuestion(question);
			user.setAnswer(answer);
			//锟斤拷锟絩equest
			if(!(username== null || username.trim().equals("")||
			   getPassword() == null || getPassword().trim().equals("")||
			   getPassword().trim().length()<6||getRepassword() == null || getRepassword().trim().equals("")||
			   !getPassword().equals(repassword)||verifyCode == null || verifyCode.trim().isEmpty()||
			   !verifyCode.equalsIgnoreCase(session_vcode))) {
				userService.registerUser(user);
				request.setAttribute("url", "login.jsp");
				request.setAttribute("info", "注册成功");
				return "register";
			}else {
				request.setAttribute("url", "register.jsp");
				request.setAttribute("info", "注册失败");
				return "fail";
			}
		} catch (Exception e) {
			request.setAttribute("url", "register.jsp");
			request.setAttribute("info", "系统异常");
			return "fail";
		}
	}

}
